#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Collections;
using FreeOnlineApplication.Class.Entity;
using FreeOnlineApplication.Class.Repository;

#endregion

namespace FreeOnlineApplication
{
    public partial class LaundryUserControl : UserControl, IChildForm
    {
        LaundryRepository m_rep;
        Laundry m_subject = new Laundry();
        IMainForm m_mainForm;
        string DECIMAL_FORMAT = "###,###,###,###,###,###,###,##0.00";

        public LaundryUserControl(string name, IMainForm mainform)
        {
            InitializeComponent();
            m_rep = new LaundryRepository(mainform.Connection);
            InitializeButtonClick();
            m_mainForm = mainform;
            this.Name = name;
            this.Dock = DockStyle.Fill;
            loadRecords();
            ClearForm();
            setEditMode(EditMode.New);
        }

        private void InitializeButtonClick()
        {
            toolBarButtonSave.Click += new EventHandler(buttonSave_Click);
            toolBarButtonEdit.Click += new EventHandler(buttonEdit_Click);
            toolBarButtonDelete.Click += new EventHandler(buttonDelete_Click);
            toolBarButtonClear.Click += new EventHandler(buttonClear_Click);
        }
        public void ClearForm()
        {
            textBoxCode.Text = "LD" + (m_rep.GetCount(m_mainForm.CurrentUnit.ID) + 1).ToString().PadLeft(3, '0');
            textBoxName.Text = "";
            numericUpDownServicePrice.Value = 0;
            m_subject = new Laundry();
            errorProvider.SetError(textBoxCode, "");
            errorProvider.SetError(textBoxName, "");
            errorProvider.SetError(numericUpDownServicePrice, "");
        }
        public void setEnableForm(bool enable)
        {
            textBoxCode.Enabled = enable;
            textBoxName.Enabled = enable;
            numericUpDownServicePrice.Enabled = enable;
        }
        public bool Valid()
        {
            bool a = textBoxCode.Text == "";
            bool d = textBoxName.Text == "";
            bool b = numericUpDownServicePrice.Value < 0;

            if (a) errorProvider.SetError(textBoxCode, "Can not Empty");
            if (b) errorProvider.SetError(numericUpDownServicePrice, "can not less then 0");
            if (d) errorProvider.SetError(textBoxName, "Can not Empty");
            return !a && !b && !d;
        }
        private void setEditMode(EditMode editmode)
        {
            toolBarButtonSave.Enabled = editmode == EditMode.New || editmode == EditMode.Update;
            toolBarButtonEdit.Enabled = editmode == EditMode.View;
            toolBarButtonDelete.Enabled = editmode == EditMode.View;
            LoadEnableButton();
        }
        private void loadRecords()
        {
            listView1.Items.Clear();
            IList records = m_rep.GetAll(m_mainForm.CurrentUnit.ID);
            foreach (Laundry d in records)
            {
                ListViewItem item = listView1.Items.Add(d.CODE);
                item.SubItems.Add(d.NAME);
                item.SubItems.Add(d.SERVICE_PRICE.ToString(DECIMAL_FORMAT));
                item.Tag = d;
            }
        }
        private void removeRecord(int id)
        {
            foreach (ListViewItem item in listView1.Items)
            {
                Laundry dep = (Laundry)item.Tag;
                if (dep.ID == id)
                {
                    listView1.Items.Remove(item);
                    break;
                }
            }
        }
        private void UpdateEntity()
        {
            m_subject.CODE = textBoxCode.Text.Trim();
            m_subject.NAME = textBoxName.Text.Trim();
            m_subject.SERVICE_PRICE = Convert.ToDouble(numericUpDownServicePrice.Value);
            m_subject.UNIT = m_mainForm.CurrentUnit;
        }
        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (Valid())
            {
                UpdateEntity();
                if (m_subject.ID == 0)
                {
                    m_rep.Insert(m_subject);
                }
                else
                {
                    m_rep.Update(m_subject);
                }
                MessageBox.Show("Data telah tersimpan.");
                ClearForm();
                loadRecords();
            }
        }
        private void buttonEdit_Click(object sender, EventArgs e)
        {
            setEnableForm(true);
            setEditMode(EditMode.Update);
        }
        private void buttonClear_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Are sure to Clear?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, new EventHandler(clear));
        }
        private void clear(object obj, EventArgs e)
        {
            MessageBoxWindow w = (MessageBoxWindow)obj;
            if (w.DialogResult == DialogResult.Yes)
            {
                ClearForm();
                setEnableForm(true);
                setEditMode(EditMode.New);
            }
        }
        private void buttonDelete_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Delete record?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, new EventHandler(delete));
        }
        private void delete(object obj, EventArgs e)
        {
            try
            {
                MessageBoxWindow w = (MessageBoxWindow)obj;
                if (w.DialogResult == DialogResult.Yes)
                {
                    if (m_subject.ID > 0)
                    {
                        m_rep.Delete(m_subject);
                        removeRecord(m_subject.ID);
                        ClearForm();
                        setEnableForm(true);
                        setEditMode(EditMode.New);
                    }
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0) return;
            ClearForm();
            m_subject = (Laundry)listView1.SelectedItem.Tag;
            loadData(m_subject);
            setEnableForm(false);
            setEditMode(EditMode.View);
        }
        private void loadData(Laundry emp)
        {
            textBoxCode.Text = emp.CODE;
            textBoxName.Text = emp.NAME;
            numericUpDownServicePrice.Value = Convert.ToDecimal(emp.SERVICE_PRICE);
        }

        #region IMainMenu Members

        public void Save()
        {
            buttonSave_Click(null, null);
        }

        public void New()
        {
            buttonClear_Click(null, null);
        }

        public void Delete()
        {
            buttonDelete_Click(null, null);
        }

        public void Edit()
        {
            buttonEdit_Click(null, null);
        }

        public void LoadEnableButton()
        {
            m_mainForm.SetEnableMenuButton(toolBarButtonSave.Enabled, toolBarButtonClear.Enabled,
            toolBarButtonDelete.Enabled, toolBarButtonEdit.Enabled);
        }

        #endregion

    }
}